package com._21cn.cms.template.utils.label;

import com._21cn.cms.common.Constants;
import com._21cn.cms.site.entity.RegionObject;
import com._21cn.cms.site.service.RegionObjectService;
import com._21cn.framework.utils.StringUtil;
import com._21cn.product.entity.*;
import com._21cn.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/28.
 */

@Component
public class RegionObjLabel {

    @Autowired
    private RegionObjectService regionObjectService;

    @Autowired
    private CarBusinessPledgeService carBusinessPledgeService;

    @Autowired(required = false)
    private CarBusinessSecondService carBusinessSecondService;

    @Autowired(required = false)
    private WatchService watchService;

    @Autowired(required = false)
    private WatchPropertyService watchPropertyService;

    @Autowired(required = false)
    private WatchCateService watchCateService;

    @Autowired(required = false)
    private CateService cateService;

    public List<CarBusinessSecond> getRegionCarSecond(int regionId, int size){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderByClause", "TOP_TIME DESC");
        params.put("eqRegionId", regionId);
        params.put("eqObjectType", Constants.REGION_OBJECT_TYPE_SECOND);
        List<RegionObject> objList= regionObjectService.getList(params, 1, size, false);
        List<Integer> inIdList = new ArrayList<Integer>();
        for(RegionObject object : objList){
            inIdList.add(object.getObjectId());
        }
        params.clear();
        params.put("inIdList", inIdList);
        params.put("eqStatus", Constants.BUSINESS_ON_SELL);
        params.put("orderByClause","find_in_set(a.id,'"+StringUtil.list2Str(inIdList)+"')");
        List<CarBusinessSecond> list = carBusinessSecondService.getCarSecondList(params, 1, size, false);
        return list;
    }

    public List<CarBusinessPledge> getRegionCarPledge(int regionId, int size){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderByClause", "TOP_TIME DESC");
        params.put("eqRegionId", regionId);
        params.put("eqObjectType", Constants.REGION_OBJECT_TYPE_PLEDGE);
        List<RegionObject> objList= regionObjectService.getList(params, 1, size, false);
        List<Integer> inIdList = new ArrayList<Integer>();
        for(RegionObject object : objList){
            inIdList.add(object.getObjectId());
        }
        params.clear();
        params.put("inIdList", inIdList);
        params.put("eqStatus", Constants.BUSINESS_ON_SELL);
        params.put("orderByClause","find_in_set(a.id,'"+StringUtil.list2Str(inIdList)+"')");
        List<CarBusinessPledge> list = carBusinessPledgeService.getCarPledgeList(params, 1, size, false);
        return list;
    }

    public List<Watch> getRegionWatch(int regionId, int size){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderByClause", "TOP_TIME DESC");
        params.put("eqRegionId", regionId);
        params.put("eqObjectType", Constants.REGION_OBJECT_TYPE_WATCH);
        List<RegionObject> objList= regionObjectService.getList(params, 1, size, false);
        List<Integer> inIdList = new ArrayList<Integer>();
        for(RegionObject object : objList){
            inIdList.add(object.getObjectId());
        }
        params.clear();
        params.put("inIdList", inIdList);
        params.put("eqStatus", Constants.BUSINESS_ON_SELL);
        params.put("orderByClause","find_in_set(id,'"+StringUtil.list2Str(inIdList)+"')");
        List<Watch> list = watchService.getList(params, 1, size, false);
        for(Watch watch : list){
            //型号
            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("eqWatchId", watch.getId());
            maps.put("eqPropertyId", 14);   //型号
            List<WatchProperty> wpList = watchPropertyService.getList(maps, 1, 1, false);
            if(!wpList.isEmpty()){
                WatchProperty watchProperty = wpList.get(0);
                watch.setModelName(watchProperty.getPropertyValue());
            }
            //品牌
            maps.clear();
            maps.put("eqWatchId", watch.getId());
            maps.put("eqTypeId", Constants.CATE_TYPE_WATCH_BRAND);
            List<WatchCate> cateList = watchCateService.getList(maps, 1, 1);
            if(!cateList.isEmpty()){
                WatchCate watchCate = cateList.get(0);
                Integer cateId = watchCate.getCateId();
                Cate cate = cateService.getById(cateId);
                watch.setBrandName(cate.getName());
            }
        }
        return list;
    }
}
